package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.mapper.ExamDetailMapper;
import com.mfl.mapper.TestExamTopicMapper;
import com.mfl.pojo.dto.ExamDetailPageQueryDTO;
import com.mfl.pojo.entity.TestExamTopic;
import com.mfl.pojo.vo.ExamDetailPageQueryVO;
import com.mfl.service.ExamDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExamDetailServiceImpl implements ExamDetailService {

    @Autowired
    private ExamDetailMapper examDetailMapper;
    @Autowired
    private TestExamTopicMapper testExamTopicMapper;

    /**
     * 分页查询考试记录详情
     * @param examDetailPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(ExamDetailPageQueryDTO examDetailPageQueryDTO) {
        PageHelper.startPage(examDetailPageQueryDTO.getPage(), examDetailPageQueryDTO.getPageSize());
        Page<ExamDetailPageQueryVO> page = examDetailMapper.pageQuery(examDetailPageQueryDTO);
        // 把正确答案和用户答案的选项转换为对应的选项信息
        page.getResult().forEach(item -> {
            // 根据试卷id和试卷题目查询试卷题目信息
            TestExamTopic testExamTopic = testExamTopicMapper.selectByTestExamIdAndTopicName(item.getTestExamId(), item.getExamTopic());
            if("A".equals(item.getTopicRightOption())){
                item.setTopicRightOption(testExamTopic.getTopicOptionA());
            }else if ("B".equals(item.getTopicRightOption())){
                item.setTopicRightOption(testExamTopic.getTopicOptionB());
            }else if ("C".equals(item.getTopicRightOption())){
                item.setTopicRightOption(testExamTopic.getTopicOptionC());
            }else if ("D".equals(item.getTopicRightOption())){
                item.setTopicRightOption(testExamTopic.getTopicOptionD());
            }
            if("A".equals(item.getTopicUserOption())){
                item.setTopicUserOption(testExamTopic.getTopicOptionA());
            }else if ("B".equals(item.getTopicUserOption())){
                item.setTopicUserOption(testExamTopic.getTopicOptionB());
            }else if ("C".equals(item.getTopicUserOption())){
                item.setTopicUserOption(testExamTopic.getTopicOptionC());
            }else if ("D".equals(item.getTopicUserOption())){
                item.setTopicUserOption(testExamTopic.getTopicOptionD());
            }
        });
        return new PageResult(page.getTotal(), page.getResult());
    }
}
